<template>
    <div style="padding: 10px ;">
        <div style=" width: 600px;">
            <div style="margin-left: 40px; margin-top: 30px; color: dodgerblue; font-size:28px ;">重置账号密码</div>
            <div style="text-align: center; width: 100%; margin-top: 15px;">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="原密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPass">
                        <el-input type="password" v-model="ruleForm.newPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="checknNewPass">
                        <el-input type="password" v-model="ruleForm.checknNewPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                        <el-button type="warning" @click.native="$router.push('/person')">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>

    </div>
</template>

<script>
import request from "@/utils/request";
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.checknNewPass !== '') {
                    this.$refs.ruleForm.validateField('checknNewPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入新密码'));
            } else if (value !== this.ruleForm.newPass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        var validatePass1 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入原密码'));
            } else if (value !== this.form.password) {
                callback(new Error('原密码不对!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                newPass: '',
                checknNewPass: '',
                pass: '',
                form: {},
            },
            rules: {
                pass: [
                    { validator: validatePass1, trigger: 'blur' }
                ],
                newPass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checknNewPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        };
    },
    mounted() {
        this.form = JSON.parse(sessionStorage.getItem("user"));
        this.loading();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.form.password == this.ruleForm.checknNewPass) {
                        alert('新原密码不能相同!');
                    } else {
                        this.form.password = this.ruleForm.checknNewPass;
                        request.put("/user", this.form).then(res => {
                            if (res.code === "0") {
                                alert('重置密码成功！');
                                this.resetForm('ruleForm');
                            }
                        });
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        loading() {
            request.get("/user/" + this.form.id).then(res => {
                this.form.password = res.data.password;
            });
        }
    }
}
</script>

<style></style>